#!/bin/bash

# 趣味英语应用部署脚本

echo "================================="
echo "趣味英语学习应用 - 部署脚本"
echo "================================="

# 配置
SERVER_USER="root"
SERVER_HOST="你的服务器IP"
SERVER_PATH="/www/wwwroot/www.lycc.top"
BACKEND_PATH="/root/fun-english-backend"

echo ""
echo "步骤 1: 构建前端..."
cd ../frontend
npm install
npm run build

if [ $? -ne 0 ]; then
    echo "❌ 前端构建失败"
    exit 1
fi

echo "✅ 前端构建成功"

echo ""
echo "步骤 2: 打包前端文件..."
tar -czf dist.tar.gz dist/

echo ""
echo "步骤 3: 上传前端到服务器..."
echo "需要手动执行以下命令："
echo "scp dist.tar.gz ${SERVER_USER}@${SERVER_HOST}:${SERVER_PATH}/"
echo "ssh ${SERVER_USER}@${SERVER_HOST} 'cd ${SERVER_PATH} && rm -rf fun-english && mkdir fun-english && tar -xzf dist.tar.gz -C fun-english --strip-components=1 && rm dist.tar.gz'"

echo ""
echo "步骤 4: 打包后端文件..."
cd ../backend
tar -czf backend.tar.gz --exclude=node_modules --exclude=.git .

echo ""
echo "步骤 5: 上传后端到服务器..."
echo "需要手动执行以下命令："
echo "scp backend.tar.gz ${SERVER_USER}@${SERVER_HOST}:${BACKEND_PATH}/"
echo "ssh ${SERVER_USER}@${SERVER_HOST} 'cd ${BACKEND_PATH} && tar -xzf backend.tar.gz && rm backend.tar.gz && npm install --production'"

echo ""
echo "步骤 6: 重启后端服务..."
echo "需要手动执行以下命令："
echo "ssh ${SERVER_USER}@${SERVER_HOST} 'cd ${BACKEND_PATH} && pm2 restart fun-english-backend || pm2 start src/index.js --name fun-english-backend'"

echo ""
echo "步骤 7: 配置Nginx..."
echo "请将 deploy/nginx.conf 的内容添加到宝塔面板的站点配置中"

echo ""
echo "================================="
echo "部署准备完成！"
echo "请按照上面的提示手动执行部署命令"
echo "================================="

